$(function () {
    //模块类型1 顶部背景框
    HiShop.DIY.Unit.event_type1 = function (ctrldom, data) {
        var $conitem = data.dom_conitem, //手机内容
            $ctrl = ctrldom, //控制内容
            tpl_con = $("#tpl_diy_con_type1").html(),//手机内容模板
            tpl_ctrl = $("#tpl_diy_ctrl_type1").html();//控制内容模板

        data.dom_ctrl = ctrldom;

        //重新渲染数据
        var reRender = function (callback) {
            var $render = $(_.template(tpl_con, data));
            $conitem.find(".members_con").remove().end().append($render);

            var $render_ctrl = $(_.template(tpl_ctrl, data));
            $ctrl.empty().append($render_ctrl);

            HiShop.DIY.Unit.event_type1($ctrl, data);
            if (callback) callback();
        }
        var setImgHeightcallback = function () {
            $('.mingoods,.biggoods').each(function (index, el) {
                var me = $(this),
                    imgHeight = me.find('img').width();
                me.find('img').height(imgHeight);
            });
        };
        //改变布局
        $ctrl.find("input[name='layout']").change(function () {
            var val = $(this).val();
            data.content.layout = val;//同步数据到缓存
            reRender(setImgHeightcallback);
        });
    };
    //模块类型2 商品选择
    HiShop.DIY.Unit.event_type2 = function (ctrldom, data) {
        let img="";
        let uploadInst = upload.render({
            elem: '.background-img'
            , multiple: false
            , exts: 'jpg|png|jpeg'
            , number: 1    //  同时上传文件的最大个数
            , url: httpurl + '/uploadImgQiniu?pathName=product&&divisor=0&dividend=1'
            , done: function (res) {
                if (res.rtnCode == 200) {//如果上传成功
                    img=visitimgurl+"/"+res.msg;
                }
                data.content.backgroundImage = img;//获取第一张图片
                reRender(setImgHeightcallback);
            }
            , error: function () {
                layer.alert("上传失败!");
            }
        });
        upload.render({
            elem: ".j-selectimg"
            , multiple: false
            , exts: 'jpg|png|jpeg'
            , number: 1    //  同时上传文件的最大个数
            , url: httpurl + '/uploadImgQiniu?pathName=other&&divisor=0&dividend=1'
            , done: function (res) {
                if (res.rtnCode == 200) {//如果上传成功
                    let gindex=0;
                    let index2=0;
                    ctrldom.find(".ctrl-item-list-li").each(function (i) {
                        let obj=$(this);
                        let indexs=i;
                        $(obj).find(".plistDiv").each(function (index,e) {
                            if($(e).hasClass("on")){
                                index2 = index;
                                console.log(index2,"index2");
                                gindex=(indexs-1);
                            }
                        })
                    })
                    data.content.goodslist[gindex].goods[index2].img = visitimgurl+"/"+res.msg;//获取第一张图片
                    reRender(setImgHeightcallback);
                }
            }
            , error: function () {
                layer.alert("上传失败!");
            }
        });
        var $conitem = data.dom_conitem, //手机内容
            $ctrl = ctrldom, //控制内容
            tpl_con = $("#tpl_diy_con_type2").html(),//手机内容模板
            tpl_ctrl = $("#tpl_diy_ctrl_type2").html();//控制内容模板

        data.dom_ctrl = ctrldom;

        //重新渲染数据
        var reRender = function (callback) {

            var $render = $(_.template(tpl_con, data));
            $conitem.find(".members_con").remove().end().append($render);
            var $render_ctrl = $(_.template(tpl_ctrl, data));
            $ctrl.empty().append($render_ctrl);

            HiShop.DIY.Unit.event_type2($ctrl, data);
            if (callback) callback();
        }
        var setImgHeightcallback = function () {
            $('.mingoods').each(function (index, el) {
                var me = $(this),
                    imgHeight = me.width();
                me.find('img').closest('a').height(imgHeight);
            });
        };
        //选择图片
        $ctrl.find(".j-selectimg").click(function () {
            $(this).parents(".plistDiv").removeClass("on");
            $(this).parents(".plistDiv").addClass("on");
        });
        //改变商品类型
        $ctrl.find(".layout input[type='radio']").change(function () {
            var index = $(this).parents("li.ctrl-item-list-li").index();
            var val = Number($(this).val());
            data.content.goodslist[index].layout = val;//同步数据到缓存
            let length = data.content.goodslist[index].goods.length;
            if(length>val){
                data.content.goodslist[index].goods.pop();
            }else if(val>length){
                let goods={
                    "img": "http://small.tlcha.com.cn/other/%E4%BA%A7%E5%93%81%E4%BA%8C.png",
                    "id": 0,
                    "name":""
                }
                data.content.goodslist[index].goods.push(goods);
            }
            reRender();
        });
        //删除商品
        $ctrl.find(".j-delgoods").click(function () {
            var index = $(this).parents("li").index();
            data.content.goodslist.splice(index, 1);
            reRender(setImgHeightcallback);
            return false;
        });
        //导航颜色选择器
        $ctrl.find(".colorPicker").click(function (e) {
            var name = $(this).data("name");
            $(this).ColorPicker({
                color: name,
                onShow: function (colpkr) {
                    $(colpkr).fadeIn(500);
                    return false;
                },
                onHide: function (colpkr) {
                    $(colpkr).fadeOut(500);
                    reRender();
                    return false;
                },
                onChange: function (hsb, hex, rgb) {
                    var hex = '#' + hex;
                    $("#wrap").css("background-color:", hex);
                    data.content.backgroundColor = hex;
                    reRender();
                }
            });
        });
        //添加商品
        $ctrl.find(".goods-select").click(function () {
            var index = $(this).parents("li.ctrl-item-list-li").index();
            let index2=$(this).data("info");
            HiShop.popbox.GoodsAndGroupPicker(function (list) {
                data.content.goodslist[index].goods[index2]=list;
                reRender(setImgHeightcallback);
            });
            return false;
        });
        //添加列表
        $ctrl.find(".ctrl-item-list-add").click(function () {
            let goods={
                "layout":1,//layout=1一行一个  layout=2 一行两个
                "goods":[
                    {
                        "img": "http://small.tlcha.com.cn/other/%E4%BA%A7%E5%93%81%E5%9B%BE%E4%B8%80.png",
                        "id": 0,
                        "name":""
                    }
                ]
            }
            data.content.goodslist.push(goods);
            reRender();
        });
        //上移
        $ctrl.find(".j-moveup").click(function () {
            var index = $(this).parents("li.ctrl-item-list-li").index();

            if (index == 0) return;//第一个导航不可再向上移动

            //替换缓存数组中的位置
            var tmpdata = data.content.goodslist.slice(index, index + 1)[0];
            data.content.goodslist.splice(index, 1);
            data.content.goodslist.splice(index - 1, 0, tmpdata);

            reRender();//更新视图
        });

        //下移
        $ctrl.find(".j-movedown").click(function () {
            var index = $(this).parents("li.ctrl-item-list-li").index(),
                len = data.content.goodslist.length;

            if (index == len - 1) return;//最后一个导航不可再向下移动

            //替换缓存数组中的位置
            var tmpdata = data.content.goodslist.slice(index, index + 1)[0];
            data.content.goodslist.splice(index, 1);
            data.content.goodslist.splice(index + 1, 0, tmpdata);

            reRender();//更新视图
        });

        //删除
        $ctrl.find(".j-del").click(function () {
            var index = $(this).parents("li.ctrl-item-list-li").index();
            data.content.goodslist.splice(index, 1);
            reRender();
        });
    };

});